var params = {
    pagenum: 1,
    pagesize: 10,
    cate_id: '',
    state: ''
}

var total = 0;

function getList() {
    $.ajax({
        url: '/my/article/list',
        data: params,
        success: function(res) {
            // console.log(res);
            total = res.total;
            var str = template('tbody_content', res)
            $('tbody').html(str);
            renderPage();
        }
    });
}
getList();
// 渲染分页内容
function renderPage() {
    layui.laypage.render({
        elem: 'page',
        count: total,
        limit: params.pagesize,
        curr: params.pagenum,
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        jump: function(obj, first) {
            if (first) return;
            // console.log(obj);
            params.pagenum = obj.curr;
            params.pagesize = obj.limit;
            getList();
        }
    });
}
// 动态渲染下拉框
$.ajax({
    url: '/my/article/cates',
    success: function(res) {
        // console.log(res);
        var str = template('select_cate', res);
        $('.cate_se').html(str);
        layui.form.render();
    }
});
// 筛选框功能
$('.filter').on('submit', function(e) {
    e.preventDefault();
    params.cate_id = $('.cate_se').val();
    params.state = $('.state').val();
    getList();
});
// 添加编辑功能
$('tbody').on('click', '.reset_id', function() {
    var id = $(this).attr('data_id')
    var value = $(this).attr('data_id');
    value = '?id=' + value;
    // console.log(value);
    console.log(location);
    // location.href = '/article/article_pud.html'

});
// 删除按钮
$('tbody').on('click', '.delete_id', function() {
    var id = $(this).attr('data_id');
    layer.confirm('确认删除此文章吗?', { icon: 3, title: '提示' }, function(index) {
        $.ajax({
            url: '/my/article/delete/' + id,
            success: function(res) {
                // console.log(res);
                layer.msg(res.message)
                getList();
            }
        })
        layer.close(index);
    });
});
// 修改时间显示
function addZero(num) {
    return num < 10 ? '0' + num : num;
}
template.defaults.imports.dateFormat = function(value) {
    var dateObj = new Date(value);
    var year = dateObj.getFullYear(),
        month = addZero(dateObj.getMonth() + 1),
        date = addZero(dateObj.getDate()),
        hour = addZero(dateObj.getHours()),
        minute = addZero(dateObj.getMinutes()),
        second = addZero(dateObj.getSeconds())
    return `${year}-${month}-${date} ${hour}:${minute}:${second}`
};